<?php
require_once($application_folder."/controllers/navigator.php");
class Cms extends navigator 
{

    function __construct()
    {
        parent::__construct();

        $this->layout->setLayout('layout_cms');
        $this->load->helper('date');

    }

    function index( $page = 'novosti' )
    { 
        
        if($this->session->userdata('logged_in')){
            $this->action($page);  
        }else{
            $this->core_cms('loginform');   
            return;
        }

    }

    function action($page){

        
        if($this->session->userdata('logged_in')){
            $this->core_cms($page, array('records' => $this->sitem->readAll($page), 'title' => $this->sitem->getTitle($page))); 
        }else{
            $this->core_cms('loginform');   
            return;
        }     


    }

    function view_text(){

        $row = $this->db->get_where('text',array('id'=>$_POST['id'],'page'=>$_POST['page']))->result_array();

        echo json_encode(array('row'=>$row));


    }

    function edit_text(){

        $action = FALSE;
        $msg = '';
        $lastid = 0;

        if($this->validation()){

            $action = TRUE;

            $id = $_POST['id'];


            if($_POST['action']=='edit'){

                $this->db->where('id', $id);
                $this->db->update('text', array('title' => $_POST['title'], 'text' => $_POST['text'], 'datetime' => time()));

            }else if($_POST['action']=='new'){

                    $newrow = array(
                    'page'=>$_POST['page'],
                    'title'=>$_POST['title'],     
                    'text'=>$_POST['text'],
                    'datetime' => time() 
                    );

                    $this->db->insert('text', $newrow);

                    $lastid = $this->db->insert_id();

                }else{
                    $action = FALSE;
                    $msg = 'Unknow error.<br />action != new && edit';
            }


        }else{

            $msg = validation_errors();

        }

        echo json_encode(array('action' => $action, 'msg' => $msg, 'lastid' => $lastid)); 


    }

    function delete_text(){
        $this->db->delete('text', array('id' => $_POST['id']));
        echo json_encode(array('msg'=>'ok')); 
    }

    function validation(){

        $this->load->library('form_validation');        

        $this->form_validation->set_rules('title', 'Naslov', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');


        $this->form_validation->set_message('required','Molimo unestite'.' <b>%s</b>!');


        if ($this->form_validation->run() == FALSE)
        {                
            return FALSE;
        }
        else
        {
            return TRUE;
        }

    }

    function language($lng){

        $this->lang_ses->setLang($lng);
        redirect( base_url().'cms' );

    }



}
